In [ ]:
from __future__ import print_function
import ipywidgets as widgets
from bqplot import pyplot as plt
from bqplot import topo_load
from bqplot.interacts import panzoom
import numpy as np
import pandas as pd
import datetime as dt

In [ ]:
# initializing data to be plotted
np.random.seed(0)
size = 100
y_data = np.cumsum(np.random.randn(size) * 100.0)
y_data_2 = np.cumsum(np.random.randn(size))
y_data_3 = np.cumsum(np.random.randn(size) * 100.)

x = np.linspace(0.0, 10.0, size)

In [ ]:
price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,
                          columns=['Security 1', 'Security 2'],
                          index=pd.date_range(start='01-01-2007', periods=150))

symbol = 'Security 1'
dates_all = price_data.index.values
final_prices = price_data[symbol].values.flatten()

In [ ]:
price_data.index.names = ['date']

Simple Plots

Line Chart


In [ ]:
plt.figure()
plt.plot(x, y_data)
plt.xlabel('Time')
plt.show()

In [ ]:
_ = plt.ylabel('Stock Price')

In [ ]:
# Setting the title for the current figure
plt.title('Brownian Increments')

In [ ]:
plt.figure()
plt.plot('Security 1', data=price_data)
plt.show()

Scatter Plot


In [ ]:
plt.figure(title='Scatter Plot with colors')
plt.scatter(y_data_2, y_data_3, color=y_data, stroke='black')
plt.show()

Horizontal and Vertical Lines


In [ ]:
## adding a horizontal line at y=0
plt.hline(0)
plt.show()

In [ ]:
## adding a vertical line at x=4 with stroke_width and colors being passed.
plt.vline(4., stroke_width=2, colors=['orangered'])
plt.show()

In [ ]:
plt.figure()
plt.scatter('Security 1', 'Security 2', color='date', data=price_data.reset_index(), stroke='black')
plt.show()

Histogram


In [ ]:
plt.figure()
plt.hist(y_data, colors=['OrangeRed'])
plt.show()

In [ ]:
plt.figure()
plt.hist('Security 1', data=price_data, colors=['MediumSeaGreen'])
plt.xlabel('Hello')
plt.show()

Bar Chart


In [ ]:
plt.figure()
bar_x=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U']
plt.bar(bar_x, y_data_3)
plt.show()

In [ ]:
plt.figure()
plt.bar('date', 'Security 2', data=price_data.reset_index()[:10])
plt.show()

Pie Chart


In [ ]:
plt.figure()
d = abs(y_data_2[:5])
plt.pie(d)
plt.show()

In [ ]:
plt.figure()
plt.pie('Security 2', color='Security 1', data=price_data[:4])
plt.show()

OHLC


In [ ]:
dates = np.arange(dt.datetime(2014, 1, 2), dt.datetime(2014, 1, 30), dt.timedelta(days=1))

prices = np.array([[ 187.21  ,  187.4   ,  185.2   ,  185.53  ],
       [ 185.83  ,  187.35  ,  185.3   ,  186.64  ],
       [ 187.15  ,  187.355 ,  185.3   ,  186.    ],
       [ 186.39  ,  190.35  ,  186.38  ,  189.71  ],
       [ 189.33  ,  189.4175,  187.26  ,  187.97  ],
       [ 189.02  ,  189.5   ,  186.55  ,  187.38  ],
       [ 188.31  ,  188.57  ,  186.28  ,  187.26  ],
       [ 186.26  ,  186.95  ,  183.86  ,  184.16  ],
       [ 185.06  ,  186.428 ,  183.8818,  185.92  ],
       [ 185.82  ,  188.65  ,  185.49  ,  187.74  ],
       [ 187.53  ,  188.99  ,  186.8   ,  188.76  ],
       [ 188.04  ,  190.81  ,  187.86  ,  190.09  ],
       [ 190.23  ,  190.39  ,  186.79  ,  188.43  ],
       [ 181.28  ,  183.5   ,  179.67  ,  182.25  ],
       [ 181.43  ,  183.72  ,  180.71  ,  182.73  ],
       [ 181.25  ,  182.8141,  179.64  ,  179.64  ],
       [ 179.605 ,  179.65  ,  177.66  ,  177.9   ],
       [ 178.05  ,  178.45  ,  176.16  ,  176.85  ],
       [ 175.98  ,  178.53  ,  175.89  ,  176.4   ],
       [ 177.17  ,  177.86  ,  176.36  ,  177.36  ]])

plt.figure()
plt.ohlc(dates, prices)
plt.show()

Boxplot


In [ ]:
plt.figure()
plt.boxplot(np.arange(10), np.random.randn(10, 100))
plt.show()

Map


In [ ]:
plt.figure()
plt.geo(map_data='WorldMap')
plt.show()

Heatmap


In [ ]:
plt.figure(padding_y=0)
plt.heatmap(x * x[:, np.newaxis])
plt.show()

GridHeatMap


In [ ]:
plt.figure(padding_y=0)
plt.gridheatmap(x[:10] * x[:10, np.newaxis])
plt.show()

Plotting Dates


In [ ]:
plt.figure()
plt.plot(dates_all, final_prices)
plt.show()

Editing existing axes properties


In [ ]:
## adding grid lines and changing the side of the axis in the figure above
_ = plt.axes(options={'x': {'grid_lines': 'solid'}, 'y': {'side': 'right', 'grid_lines': 'dashed'}})

Advanced Usage

Multiple Marks on the same Figure


In [ ]:
plt.figure()
plt.plot(x, y_data_3, colors=['orangered'])
plt.scatter(x, y_data, stroke='black')
plt.show()

Using marker strings in Line Chart


In [ ]:
mark_x = np.arange(10)
plt.figure(title='Using Marker Strings')
plt.plot(mark_x, 3 * mark_x + 5, 'g-.s') # color=green, line_style=dash_dotted, marker=square
plt.plot(mark_x ** 2, 'm:d') # color=magenta, line_style=None, marker=diamond
plt.show()

Partially changing the scales


In [ ]:
plt.figure()
plt.plot(x, y_data)

## preserving the x scale and changing the y scale
plt.scales(scales={'x': plt.Keep})
plt.plot(x, y_data_2, colors=['orange'], axes_options={'y': {'side': 'right', 'color': 'orange',
                                                             'grid_lines': 'none'}})
plt.show()

Adding a label to the chart


In [ ]:
plt.figure()
line = plt.plot(dates_all, final_prices)
plt.show()

In [ ]:
## adds the label to the figure created above
_ = plt.label(['Pie Day'], x=[np.datetime64('2007-03-14')], y=[final_prices.mean()], scales=line.scales,
              colors=['orangered'])

Changing context figure


In [ ]:
plt.figure(1)
plt.plot(x,y_data_3)
plt.show()

In [ ]:
plt.figure(2)
plt.plot(x[:20],y_data_3[:20])
plt.show()

Re-editing first figure


In [ ]:
## adds the new line to the first figure
fig = plt.figure(1, title='New title')
plt.plot(x,y_data, colors=['orangered'])
fig

Viewing the properties of the figure


In [ ]:
marks = plt.current_figure().marks
marks[0].get_state()

Showing a second view of the first figure


In [ ]:
plt.show()

Clearing the figure


In [ ]:
### Clearing the figure above
plt.clear()

Deleting a figure and all its views.


In [ ]:
plt.show(2)

In [ ]:
plt.close(2)

Interactions in Pyplot

Brush Selector


In [ ]:
fig = plt.figure()
plt.scatter(y_data_2, y_data_3, colors=['orange'], stroke='black')

label = widgets.Label()

def callback(name, value):
    label.value = str(value)
## click and drag on the figure to see the selector
plt.brush_selector(callback)

widgets.VBox([fig, label])

Fast Interval Selector


In [ ]:
fig = plt.figure()
n = 100
plt.plot(np.arange(n), y_data_3)
label = widgets.Label()

def callback(name, value):
    label.value = str(value)

## click on the figure to activate the selector
plt.int_selector(callback)

widgets.VBox([fig, label])

Brush Interval Selector with call back on brushing


In [ ]:
# click and drag on chart to make a selection
def callback(name, value):
    label.value = 'Brushing: ' + str(value)
_ = plt.brush_int_selector(callback, 'brushing')